home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac Power 1997 December
/
MACPOWER-1997-12.ISO.7z
/
MACPOWER-1997-12.ISO
/
AMUG
/
PROGRAMMING
/
Raven 1.2 Examples.sit
/
Raven 1.2 Examples
/
BoxPaint
/
Source
/
Texture.cpp
< prev
next >
Wrap
Text File
|
1997-03-09
|
4KB
|
166 lines
/*
* File: Texture.cpp
* Summary: T3DPixmapTexture than can be drawn onto.
* Written by: Jesse Jones
*
* Copyright ゥ 1997 Jesse Jones.
* For conditions of distribution and use, see copyright notice in ZTypes.h
*
* Change History (most recent first):
*
* <-> 3/09/97 JDJ Created.
*/
#include "Texture.h"
#include <QD3DStorage.h>
#include <Z3DVectors.h>
#include <Z3DUtils.h>
// ===================================================================================
// class CTextureMap
// ===================================================================================
//---------------------------------------------------------------
//
// CTextureMap::~CTextureMap
//
//---------------------------------------------------------------
CTextureMap::~CTextureMap()
{
}
//---------------------------------------------------------------
//
// CTextureMap::CTextureMap
//
//---------------------------------------------------------------
CTextureMap::CTextureMap(const TPicture& pict, ulong resolution) : T3DPixmapTexture(SPixmapStorage(pict, resolution, resolution, 32))
{
mPict = pict;
}
//---------------------------------------------------------------
//
// CTextureMap::SetResolution
//
//---------------------------------------------------------------
void CTextureMap::SetResolution(ulong resolution)
{
if (resolution != this->GetResolution()) {
SPixmapStorage pixmap(mPict, resolution, resolution, 32);
this->SetPixmap(pixmap);
}
}
//---------------------------------------------------------------
//
// CTextureMap::SetPict
//
//---------------------------------------------------------------
void CTextureMap::SetPict(const TPicture& pict)
{
mPict = pict;
SPixmapStorage pixmap(mPict, this->GetWidth(), this->GetHeight(), 32);
this->SetPixmap(pixmap);
}
//---------------------------------------------------------------
//
// CTextureMap::DrawLine (TQ3Param2D, TQ3Param2D)
//
//---------------------------------------------------------------
void CTextureMap::DrawLine(const TQ3Param2D& fromUV, const TQ3Param2D& toUV)
{
TPoint size, from, to;
size.h = this->GetWidth();
size.v = this->GetHeight();
from.h = size.v*fromUV.u;
from.v = size.h*(1.0 - fromUV.v);
to.h = size.v*toUV.u;
to.v = size.h*(1.0 - toUV.v);
this->DrawLine(from, to);
}
#pragma mark ハ
//---------------------------------------------------------------
//
// CTextureMap::DrawLine (TPoint, TPoint)
//
//---------------------------------------------------------------
void CTextureMap::DrawLine(const TPoint& from, const TPoint& to)
{
const double kPixelSpacing = 1.0;
const double kPixelSpacingSqr = kPixelSpacing*kPixelSpacing;
const double kMinSpacing = 0.001;
SPixmapStorage pixmap = this->GetPixmap();
if (from == to)
this->SetPixel(pixmap, from);
else {
T2DVector srcVector(to.h - from.h, to.v - from.v);
// Find distance from old to new pixel
double pixelDistSqr = srcVector.LengthSquared();
double spacing = kPixelSpacingSqr/pixelDistSqr;
if (spacing < kMinSpacing)
spacing = kMinSpacing;
TPoint lastPixel(16000, 16000);
for (double t = 0.0; t <= 1.0; t += spacing) {
TPoint pixel;
pixel.h = from.h + t*srcVector.x;
pixel.v = from.v + t*srcVector.y;
if (pixel != lastPixel) {
this->SetPixel(pixmap, pixel);
lastPixel = pixel;
}
}
}
// Q3PixmapTexture_GetPixmap returns a copy of the pixel
// image so we have to give QD the new version.
this->SetPixmap(pixmap);
}
//---------------------------------------------------------------
//
// CTextureMap::SetPixel
//
//---------------------------------------------------------------
void CTextureMap::SetPixel(SPixmapStorage& pixmap, const TPoint& pixel)
{
ASSERT(pixmap.pixelSize == 32); // code assumes this is true
long offset = 4*pixel.h + pixel.v*pixmap.rowBytes;
if (offset >= 0 && offset < pixmap.height*pixmap.rowBytes) {
long value = 0xFF000000L; // opaque and black
ulong bytesWritten;
TQ3Status status = Q3Storage_SetData(pixmap.image, offset, 4, (Byte*) &value, &bytesWritten);
ThrowIf3DError(status);
}
}